package com.study.map.service.impl;

import com.alibaba.fastjson.JSON;
import com.study.map.constant.GaodeMapConstants;
import com.study.map.dto.RouteWalkingRequest;
import com.study.map.dto.RouteWalkingResponse;
import com.study.map.exception.MapServiceException;
import com.study.map.service.IRouteService;
import com.study.map.util.MapHttpUtil;
import com.study.map.config.GaodeMapConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 路径规划服务实现类
 *
 * @author study
 * @date 2025-08-31
 */
@Slf4j
@Service
public class RouteServiceImpl implements IRouteService {

    @Autowired
    private GaodeMapConfig gaodeMapConfig;

    @Autowired
    private MapHttpUtil mapHttpUtil;

    /**
     * 路径规划：根据起点和终点规划路径
     *
     * @param request 路径规划请求参数
     * @return 路径规划结果
     * @throws MapServiceException 地图服务异常
     */
    @Override
    public RouteWalkingResponse routePlan(RouteWalkingRequest request) throws MapServiceException {
        try {
            // 根据类型确定请求URL
            String url;
            switch (request.getType()) {
                case "walking":
                    url = gaodeMapConfig.getApiUrl() + "/" + GaodeMapConstants.API_VERSION_V4 + GaodeMapConstants.WALKING_ROUTE_URL;
                    break;
                case "bicycling":
                    url = gaodeMapConfig.getApiUrl() + "/" + GaodeMapConstants.API_VERSION_V4 + GaodeMapConstants.BICYCLING_ROUTE_URL;
                    break;
                case "transit":
                    url = gaodeMapConfig.getApiUrl() + "/" + GaodeMapConstants.API_VERSION_V3 + GaodeMapConstants.TRANSIT_ROUTE_URL;
                    break;
                default: // driving
                    url = gaodeMapConfig.getApiUrl() + "/" + GaodeMapConstants.API_VERSION_V3 + GaodeMapConstants.DRIVING_ROUTE_URL;
                    break;
            }

            // 构建请求参数
            Map<String, Object> params = new HashMap<>();
            if (StringUtils.hasText(request.getOrigin())) {
                params.put("origin", request.getOrigin());
            }
            if (StringUtils.hasText(request.getDestination())) {
                params.put("destination", request.getDestination());
            }
            params.put("output", request.getOutput());

            // 发送HTTP请求
            RouteWalkingResponse response = mapHttpUtil.doGet(url, params, RouteWalkingResponse.class);

            // 解析响应结果
//            RouteResponse response = JSON.parseObject(responseStr, RouteResponse.class);

            // 检查响应状态
            if (!GaodeMapConstants.STATUS_SUCCESS.equals(response.getStatus())) {
                log.error("路径规划请求失败，状态码: {}, 信息: {}", response.getStatus(), response.getInfo());
                throw new MapServiceException("路径规划请求失败: " + response.getInfo());
            }

            return response;
        } catch (Exception e) {
            log.error("路径规划请求异常", e);
            throw new MapServiceException("路径规划请求异常", e);
        }
    }

    /**
     * 路径规划：根据起点和终点规划路径
     *
     * @param origin 出发点坐标
     * @param destination 目的地坐标
     * @param type 路径规划类型（driving, walking, bicycling, transit）
     * @return 路径规划结果
     * @throws MapServiceException 地图服务异常
     */
    @Override
    public RouteWalkingResponse routePlan(String origin, String destination, String type) throws MapServiceException {
        RouteWalkingRequest request = new RouteWalkingRequest();
        request.setOrigin(origin);
        request.setDestination(destination);
        request.setType(type);
        return routePlan(request);
    }
}